package net.seabookchen.controller;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.seabookchen.dao.WishDao;
import net.seabookchen.pojo.Wish;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.google.appengine.api.datastore.EntityNotFoundException;

@Controller
public class WishController extends CommonController<Wish> {
	private static final Logger log = Logger.getLogger(WishController.class.getName());
	private WishDao wDao;
	
	public WishController() {
		if (wDao == null) wDao = new WishDao(Wish.class);
	}
	
	@RequestMapping(value ="/wish", method=RequestMethod.POST)
	public ModelAndView makeWish(HttpServletRequest request, HttpServletResponse resp, Model model) {
		String wishStr = request.getParameter("wish");
		Wish wish = new Wish(wishStr);
		wDao.put(wish);
		populateModel(model, wish, RequestMethod.POST + "-wish-" + wish.getId());
		log.info("Make a Wish: " + wish);
		return new ModelAndView("restful");
	}
	
	@RequestMapping(value ="/allwishes", method=RequestMethod.GET)
	public ModelAndView listWishes(HttpServletRequest request, HttpServletResponse resp, Model model) {
		List<Wish> wishes = wDao.listWishes();
		model.addAttribute("entities", wishes);
		log.info("List All Wishes size: " + wishes.size());
		return new ModelAndView("showWishes");
	}
	
	@RequestMapping(value ="/allwishes/{index}", method=RequestMethod.GET)
	public ModelAndView listWishes(HttpServletRequest request, HttpServletResponse resp, @PathVariable String index, Model model) {
		List<Wish> wishes = wDao.listWishes(Integer.valueOf(index));
		model.addAttribute("entities", wishes);
		log.info("List All Wishes size: " + wishes.size());
		return new ModelAndView("showWishes");
	}
	
	@RequestMapping(value ="/wish/{id}", method=RequestMethod.GET)
	public ModelAndView getWish(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		Wish wish = null;
		try {
			wish = wDao.get(Long.parseLong(id));
		} catch (NumberFormatException e) {
			log.severe(e.getMessage());
		} catch (EntityNotFoundException e) {
			log.severe(e.getMessage());
		}
		populateModel(model, wish, RequestMethod.GET + "-Wish-" +id);
		log.info("GET Wish: " + wish);
		return new ModelAndView("restful");
	}
	
	@RequestMapping(value ="/wish/{id}", method=RequestMethod.PUT)
	public ModelAndView updateWish(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		String wisthStr = request.getParameter("wish");
		Wish wish = null;
		try {
			wish = wDao.get(Long.parseLong(id));
			wish.setWish(wisthStr);
			wDao.put(wish);
		} catch (NumberFormatException e) {
			log.severe(e.getMessage());
		} catch (EntityNotFoundException e) {
			log.severe(e.getMessage());
		}
		populateModel(model, wish, RequestMethod.GET + "-Wish-" +id);
		log.info("Put Wish: " + wish);
		return new ModelAndView("restful");
	}
	
	@RequestMapping(value ="/wish/{id}", method=RequestMethod.DELETE)
	public ModelAndView deleteWish(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		wDao.delete(Long.parseLong(id));
		model.addAttribute("action", RequestMethod.DELETE + "-Wish-" +id);
		log.info("Delete Wish: " + id);
		return new ModelAndView("restful");
	}
}
